/*
 * =====================================================================================
 *
 *       Filename:  util_time.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2020年02月20日 17时09分48秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef _UTIL_TIME_H_
#define _UTIL_TIME_H_

struct period {
    struct timeval start;
    struct timeval finish;
};

#define PERIOD_SET_START(period) \
    gettimeofday(&((period)->start), NULL)

#define PERIOD_SET_FINISH(period) \
    gettimeofday(&((period)->finish), NULL)

#define PERIOD_SET_FINISH_TO_START(period) \
    memcpy(&((period)->finish), &((period)->start), sizeof((period)->start))

#define PERIOD_DELTA_SEC(period) \
    ((period)->finish.tv_sec - (period)->start.tv_sec)

#define PERIOD_DELTA_USEC(period) \
    ((period)->finish.tv_usec - (period)->start.tv_usec)

#define PERIOD_VALUE_SEC(period) \
    PERIOD_DELTA_SEC(period)

#define PERIOD_VALUE_MSEC(period) \
    ((PERIOD_DELTA_SEC(period)*1000) + (PERIOD_DELTA_USEC(period)/1000))

#define PERIOD_VALUE_USEC(period) \
    ((PERIOD_DELTA_SEC(period)*1000*1000) + PERIOD_DELTA_USEC(period))

#define PERIOD_PRINT(period, hint) \
        printf("%s: %lu.%lu seconds\n", (hint)?(hint):"",\
        PERIOD_DELTA_SEC(period), PERIOD_DELTA_USEC(period))

#define PERIOD_PRINT_WITH_TIME(period, hint) \
        printf("%s: %lu.%lu seconds, Start At:%lu.%lu Finish At:%lu.%lu\n", (hint)?(hint):"",\
        PERIOD_DELTA_SEC(period), PERIOD_DELTA_USEC(period) \
        (period)->start.tv_usec, (period)->start.tv_usec, \
        (period)->finish.tv_usec, (period)->finish.tv_usec)

#endif

